package com.xingguo.guide;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.base.component.BaseAction;
import com.base.component.BaseHibernateDAO;
import com.base.component.Constant;
import com.base.component.MessageConstant;
import com.base.component.Pager;
import com.opensymphony.xwork2.ModelDriven;
import com.xingguo.account.AccountAction;

@Controller
public class GuideAction extends BaseAction implements ModelDriven<Guide>{
	@Resource
	BaseHibernateDAO baseHibernateDAO;
	
	private Pager pager = new Pager();
	private Integer currentId;
	private String[] imgNameList;
	private String[] imgURLList;
	private String[] idArray;
	
	Guide guide = new Guide();
	
	static Logger logger = LogManager.getLogger(AccountAction.class.getName());
	
	@SuppressWarnings("unchecked")
	public String search(){
		String title = "";
		title = pager.getValues()[0];
		String hql = "from Guide a where a.type = "+guide.getType()+" and a.title like '%"+title+"%'";
		pager.setProps(new String[]{"title"});
		Long totalSize = baseHibernateDAO.countByHQL("select count(*)"+hql);
		pager.setTotalSize(totalSize);
		System.out.println(pager);
		int offSet = pager.getOffset();
		int pageSize = pager.getSize();
		List<Guide> list = baseHibernateDAO.findByHQL(hql, offSet, pageSize);
		return addAttr("list", list).forward("list.jsp");
	}
	private HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	private List<GuideImage> getImages(){
		
		List<GuideImage> imgList = new ArrayList<GuideImage>();
		if(null == imgNameList){
			return imgList;
		}
		int imgCount = imgNameList.length;
		for(int i=0;i<imgCount;i++){
			GuideImage image = new GuideImage();
			image.setImgName(imgNameList[i]);
			image.setUrl(imgURLList[i]);
			imgList.add(image);
		}
		return imgList;
	}
	
	public String goToAdd(){
		Guide tempGuide = new Guide();
		tempGuide.setType(guide.getType());
		return addAttr("tempGuide", tempGuide).forward("edit.jsp");
	}
	
	public String add(){
		guide.setAddDate(new Date());
		guide.setImages(getImages());
		baseHibernateDAO.save(guide);
		logger.info("add:{}", guide);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.SAVE_SUCCESS);
		return search();
	}

	public String goToEdit(){
		Guide tempGuide = (Guide) baseHibernateDAO.get(Guide.class, currentId);
		return addAttr("tempGuide", tempGuide).forward("edit.jsp");
	}
	
	public String edit(){
		guide.setImages(getImages());
		baseHibernateDAO.update(guide);
		logger.info("edit:{}", guide);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.EDIT_SUCCESS);
		return search();
	}
	
	public String delete() {
		baseHibernateDAO.delete(guide);
		logger.info("delete:{}",guide);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.DELETE_SUCCESS);
		return search();
	}
	
	public String deleteBatch(){
		for(String id:idArray){
			Guide tempGuide = new Guide();
			tempGuide.setId(Integer.valueOf(id));
			baseHibernateDAO.delete(tempGuide);
		}
		logger.info("deleteBatch:{}",(Object[])idArray);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.DELETE_SUCCESS);
		return search();
	}
	
	@Override
	public Guide getModel() {
		return guide;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public Integer getCurrentId() {
		return currentId;
	}

	public void setCurrentId(Integer currentId) {
		this.currentId = currentId;
	}

	public String[] getIdArray() {
		return idArray;
	}

	public void setIdArray(String[] idArray) {
		this.idArray = idArray;
	}

	public String[] getImgNameList() {
		return imgNameList;
	}

	public void setImgNameList(String[] imgNameList) {
		this.imgNameList = imgNameList;
	}

	public String[] getImgURLList() {
		return imgURLList;
	}

	public void setImgURLList(String[] imgURLList) {
		this.imgURLList = imgURLList;
	}
	
}
